#include "xftp_server_cmd.h"

#include <event2/bufferevent.h>
#include <string.h>
#include <stdio.h>
#include <string>

#include <iostream>
using namespace std;

XFtpServerCmd::XFtpServerCmd() {
}

XFtpServerCmd::~XFtpServerCmd() {
	Close();
	for (auto ptr = calls_del.begin(); ptr != calls_del.end(); ++ptr) {
		ptr->first->Close();
		delete ptr->first;
	}
}

// 子线程XThread event事件分发中
void XFtpServerCmd::Read(struct bufferevent *bev) {
    char data[1024] = {0};
    for (;;) {
        int len = bufferevent_read(bev, data, sizeof(data) - 1);
        if (len <= 0) break;

        data[len] = '\0';
        cout << "Recv Cmd data:" << data << flush << endl;
        // 分发到处理对象
        // 分析出类型
        string type = "";
        for (int i = 0; i < len; ++i) {
            if (data[i] == ' ' || data[i] == '\r') {
                break;
            }
            type += data[i];
        }
        cout << "type is [" << type << endl;

        if (calls.find(type) != calls.end()) {
            XFtpTask *t = calls[type];
            t->cmdTask = this; // 用来处理回复命令和目录
            t->ip = ip;
            t->port = port;
            t->base = base;
            t->Parse(type, data);

            if (type == "PORT") {
                ip = t->ip;
                port = t->port;
            }
        } else {
            string msg = "200 OK\r\n";
            bufferevent_write(bev, msg.c_str(), msg.size());
        }
    }
}

void XFtpServerCmd::Event(struct bufferevent *bev, short what) {
    // 如果对方网络断掉,或者机器死机有可能收不到BEV_EVENT_EOF数据
    if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
        cout << "BEV_EVENT_EOF | BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT" << endl;
        delete this;
    }
}

// 初始化任务, 运行在子线程中
bool XFtpServerCmd::Init() {
    cout << "XFtpServerCmd::Init()" << endl;
    // 监听socket bufferevent
    bufferevent *bev = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE); 
	if (!bev) {
		delete this;
		return false;
	}
    this->bev = bev;  
    this->SetCallback(bev);
    
    // 添加超时
    timeval rt = {60, 0};
    bufferevent_set_timeouts(bev, &rt, 0);

    string msg = "220 Welcome to libevent XFtpServer\r\n";
    bufferevent_write(bev, msg.c_str(), msg.size());
    return true;
}

// 注册命令处理对象,不需要考虑线程安全,调用时还未分发到线程
void XFtpServerCmd::Reg(std::string cmd, XFtpTask *call) {
    if (!call) {
        cout << "XFtpServerCmd::Reg call is null" << endl;
        return;
    }

    if (cmd.empty()) {
        cout << "XFtpServerCmd::Reg cmd is null" << endl;
        return;
    }

    // 已经注册的是否覆盖,不覆盖,提示错误
    if (calls.find(cmd) != calls.end()) {
        cout << cmd << " is already register" << endl;
        return;
    }

    calls[cmd] = call;
	calls_del[call] = 0;
}
